package com.villaday.idf;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.text.Html;
import android.util.Log;

import androidx.multidex.MultiDex;

import com.atuan.datepickerlibrary.CalendarUtil;
import com.bumptech.glide.request.target.ViewTarget;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.commonsdk.utils.UMUtils;
import com.villaday.idf.util.DateUtils;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.LongLogUtils;
import com.villaday.idf.util.PushHelper;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.Util;
/*
import com.wenming.library.LogReport;
import com.wenming.library.save.imp.CrashWriter;
import com.wenming.library.upload.email.EmailReporter;
*/

import com.villaday.idf.util.ConfigConstants;
import com.villaday.idf.util.OkHttpUtil;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;


public class AppContext extends Application {

    private int cartCount = 0;
    private static AppContext instance;
    public static boolean isHightWX = false; //是否是高版本微信

    public static String currentCityidCurrent = "";//当前城市id
    public static String cityCurrent = "定位中";//当前城市

    public static String getuiPushId = "";//个推id

    public static String JPushId = "";//极光id

    public static double latitude = 22.548554;    //获取纬度信息
    public static double longitude = 114.065944;    //获取经度信息
    public static String CityName = "无法定位";//极光id

    public static boolean isLikeChange = false; //是否需要刷新动态

    //全局日期
    public static int startGroup = -1;
    public static int startChild = -1;
    public static int endGroup = -1;
    public static int endChild = -1;
    public static String checkInDate = "";
    public static String checkOutDate = "";

    //客户id
    public static int customId = -1;//

    @Override
    public void onCreate() {
        super.onCreate();

        try {
            if (DateUtils.isMoreThan6points(18)) {
                checkInDate = DateUtils.getCalculateDay(null, 0);
                checkOutDate = DateUtils.getCalculateDay(checkInDate, 1);

            } else {
                checkInDate = DateUtils.getCalculateDay(new Date(), 1);
                checkOutDate = DateUtils.getCalculateDay(checkInDate, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            LongLogUtils.e("eee", e.getMessage());
        }

        ViewTarget.setTagId(R.id.itemTag);

        Fresco.initialize(this, ConfigConstants.getImagePipelineConfig(this));
        //initCrashReport();

        instance = this;
        OkHttpUtil.initOkHttp();


    }

    public void initPush() {
        String wxVersionCode = Util.getWXVersionCode(this);
        String[] split = wxVersionCode.split("\\.");
        if (Integer.parseInt(split[0]) > 6) {
            isHightWX = true;
        } else if (Integer.parseInt(split[0]) == 6 && Integer.parseInt(split[1]) > 7) {
            isHightWX = true;
        } else if (Integer.parseInt(split[0]) == 6 && Integer.parseInt(split[1]) == 7 && Integer.parseInt(split[2]) > 2) {
            isHightWX = true;
        }

//        ZXingLibrary.initDisplayOpinion(this);
        LogeUtil.setDebug(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
// com.getui.demo.DemoPushService 为第三方自定义推送服务
      /*  PushManager.getInstance().initialize(this.getApplicationContext(), GeTuiPushService.class);

        // com.getui.demo.DemoIntentService 为第三方自定义的推送服务事件接收类
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), GeTuiIntentService.class);*/


        JPushInterface.setDebugMode(false);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);            // 初始化 JPush

     /*   UMConfigure.setLogEnabled(true);
        //UMConfigure.init(this, "5bd1259ff1f556614b000013", "安智市场", UMConfigure.DEVICE_TYPE_PHONE,null );

        UMConfigure.init(this, "5bd1259ff1f556614b000013", "官网", UMConfigure.DEVICE_TYPE_PHONE, null);

        CrashReport.initCrashReport(getApplicationContext(), "a3d0729c3f", false);*/
        initUmengSDK();

    }

    /**
     * 初始化友盟组件
     */
    private void initUmengSDK() {
        //日志开关
        UMConfigure.setLogEnabled(false);
        //预初始化
        PushHelper.preInit(this);
       /* //判断是否已同意隐私政策
        boolean agreed = ...;
        if (!agreed) {
            return;
        }*/
        boolean isMainProcess = UMUtils.isMainProgress(this);
        if (isMainProcess) {
            //App启动速度优化：可以在子线程中调用SDK初始化接口
            new Thread(new Runnable() {
                @Override
                public void run() {
                    PushHelper.init(getApplicationContext());
                }
            }).start();
        }
    }

 /*   private void initCrashReport() {
        getApplicationContext();
        LogReport.getInstance()
                .setCacheSize(30 * 1024 * 1024)//支持设置缓存大小，超出后清空
                .setLogDir(getApplicationContext(), "sdcard/" + this.getString(this.getApplicationInfo().labelRes) + "/")//定义路径为：sdcard/[app name]/
                .setWifiOnly(true)//设置只在Wifi状态下上传，设置为false为Wifi和移动网络都上传
                .setLogSaver(new CrashWriter(getApplicationContext()))//支持自定义保存崩溃信息的样式
                //.setEncryption(new AESEncode()) //支持日志到AES加密或者DES加密，默认不开启
                .init(getApplicationContext());
        //initEmailReporter();
    }*/

   /* *//**
     * 使用EMAIL发送日志
     *//*
    private void initEmailReporter() {
        EmailReporter email = new EmailReporter(this);
        email.setReceiver("wenmingvs@gmail.com");//收件人
        email.setSender("wenmingvs@163.com");//发送人邮箱
        email.setSendPassword("apptest1234");//邮箱的客户端授权码，注意不是邮箱密码
        email.setSMTPHost("smtp.163.com");//SMTP地址
        email.setPort("465");//SMTP 端口
        LogReport.getInstance().setUploadType(email);
    }
*/
    public int getCartCount() {
        return cartCount;
    }

    public void setCartCount(int cartCount) {
        this.cartCount = cartCount;
    }

    /**
     * app退出时调用
     */
    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    public static AppContext getInstance() {
        // 这里不用判断instance是否为空
        return instance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}